package com.cq.oa.web.sm.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oa.base.session.SessionManger;
import oa.base.util.Constants;
import oa.base.util.Page;
import oa.base.web.Action.BaseAction;

import com.cq.oa.domain.sm.Org;
import com.cq.oa.service.sm.OrgService;
import com.cq.oa.utils.TreeNodeUtils;

public class SmOrgAction extends BaseAction<Org>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2283944864146115800L;
	
	private OrgService orgService;
	

	public void setOrgService(OrgService orgService) {
		this.orgService = orgService;
	}

	@Override
	protected Class<Org> getEntityClass() {
		return Org.class;
	}
	
	public String findByPage() throws Exception {
		Page<Org> users = orgService.findByPage(filterMap, linkMap,shortMap, super.getPageNo(),
				super.getPageSize());
		this.renderJsonSuccess(jsonUtils.getExtJson(users, new String[] {}));
		return JSON;
	}
	public String operate(){
		if(null!=entity.getSid()){
			entity = orgService.findByParmkeys(entity.getSid());
		}
		return "operate";
	}
	
	public String save(){
		entity.setOrgSid(SessionManger.getOrgId());
		if(orgService.isUnique(entity, "ename,orgSid")){
			entity.setEmail(entity.getEname()+"@"+SessionManger.getDomain());
			if(null!=entity.getSid()){
				this.orgService.update(entity);
			}else{
				this.orgService.create(entity);
			}
			this.renderJsonSuccess();
		}else{
			this.renderJsonError("部门编码重复，请重新设置，本编码用于部门邮件！");
		}
		return JSON;
	}
	
	public String delete(){
		this.orgService.delete(entity.getSid());
		this.renderJsonSuccess();
		return JSON;
	}
	
	
	public String findChildTree() throws Exception{
		if(id==null){
			id="0";
		}
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("upId", Long.valueOf(id));
		params.put("orgSid", SessionManger.getOrgId());
		List<Org> orgs=orgService.findParByList(params);
		this.renderJsonSuccess(super.jsonUtils.getExtArrayJson(TreeNodeUtils.bulid2TreeNode(orgs)));
		return JSON;
	}
	/**
	 * 组织部门关系
	 * @return
	 * @throws Exception
	 * lilang 
	 * 2013-10-18上午10:27:30
	 */
	public String findCompanyTree() throws Exception{
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("orgSid", SessionManger.getOrgId());
		Map<String,Object> orders = new HashMap<String,Object>();
		orders.put(Constants.ORDER_BY_ASC, "orderBy");
		List<Org> orgs=orgService.findByParams(params, orders);
		this.renderJsonSuccess(super.jsonUtils.getExtArrayJson(TreeNodeUtils.bulid2TreeNode(orgs,0L)));
		return JSON;
	}
	
	/**
	 * 部门树
	 * @return
	 * lilang 
	 * 2013-10-18上午10:37:02
	 */
	public String deptSelect(){
		return "deptSelect";
	}
	
}
